/**
 * utils方法
 */
// 统一批量导出
const utils = {
    deepClone: (obj) => {
        return deepClone(obj)
    },
    saveExcelFile: (data,fName) => {
        saveExcelFile(data,fName)
    }
}
export default utils;

/** 深克隆*/
function deepClone (source) {
    if (!source && typeof source !== 'object') {
        throw new Error('error arguments', 'deepClone')
    }
    const targetObj = source.constructor === Array ? [] : {}
    Object.keys(source).forEach(keys => {
        if (source[keys] && typeof source[keys] === 'object') {
            targetObj[keys] = deepClone(source[keys])
        } else {
            targetObj[keys] = source[keys]
        }
    })
    return targetObj
}

/** 下载excel*/
function saveExcelFile(data,fName) {
    let fileName = fName||'未命名'
    // 判断空
    if (utils.isEmpty(fileName)) return;
    const blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
    // 输出文件
    if (window.navigator.msSaveBlob) {
        window.navigator.msSaveBlob(blob, fileName);
    } else {
        var a = document.createElement('a');
        a.href = URL.createObjectURL(blob);
        a.target = '_blank';
        a.download = fileName;
        a.click();
        URL.revokeObjectURL(a.href);
    }
}

